﻿package core.drawing
{
    import core.enum.*;
    import core.image.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class ShapeTool extends Tool
    {
        public var shape:Brush;
        public var offset:Point;
        public var target:BitmapData;
        var i:int;
        var xinc:int;
        var yinc:int;
        var cumul:int;
        var p:Point;
        var x:int = -1;
        var y:int = -1;
        var dx:int;
        var dy:int;
        var lx:int;
        var ly:int;

        public function ShapeTool(param1:Tuyitu, param2:ToolType)
        {
            super(param1, param2, false);
            param1.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.KeyDown, false, 0, true);
            return;
        }// end function

        public function Prepare(MOUSE_UP:Action, MOUSE_UP:BitmapData) : void
        {
            history.Prepare(MOUSE_UP);
            ed.cursor.freeze = true;
            ed.addEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false, 1, true);
            ed.addEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false, 1, true);
            this.target = MOUSE_UP;
            this.shape = options.shape;
            this.offset = layer.offset;
            return;
        }// end function

        override public function MouseUp(event:MouseEvent) : void
        {
            ed.cursor.freeze = false;
            ed.removeEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false);
            ed.removeEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false);
            layer.RemoveBitmapEditMode();
            canvas.UpdateAfterEdit();
            history.Commit();
            if (!AppState.OnCanvas)
            {
                ed.cursor.Set(ToolType.None);
            }
            return;
        }// end function

        public function KeyDown(event:KeyboardEvent) : void
        {
            var _loc_2:* = undefined;
            if (event.keyCode == 190 || event.keyCode == 188)
            {
                _loc_2 = event.shiftKey ? (event.keyCode + 100) : (event.keyCode);
                ed.options.selected.bp.SetShapeProperty(_loc_2);
                if (AppState.OnCanvas)
                {
                    ed.cursor.Set(type);
                }
            }
            return;
        }// end function

        override public function Reset() : void
        {
            type.attitude.locked = Boolean(ed.canvas.layer.type != LayerType.Bitmap);
            if (AppState.OnCanvas)
            {
                ed.cursor.Set(type);
            }
            ed.options.SetTool(type);
            return;
        }// end function

        override public function CleanUp() : void
        {
            ed.stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.KeyDown, false);
            type.attitude.state = "normal";
            return;
        }// end function

        override public function MouseDrag(event:MouseEvent) : void
        {
            if (this.target != null)
            {
                this.p = canvas.GetOffsetMousePosition(this.offset);
                this.target.lock();
                if (this.x == -1)
                {
                    this.lx = this.p.x;
                    this.ly = this.p.y;
                    this.x = this.p.x;
                    this.y = this.p.y;
                    this.DrawShape(this.x, this.y);
                }
                if (this.p.x != this.x || this.p.y != this.y)
                {
                    this.dx = this.p.x - this.x;
                    this.dy = this.p.y - this.y;
                    this.xinc = this.dx > 0 ? (1) : (-1);
                    this.yinc = this.dy > 0 ? (1) : (-1);
                    this.dx = this.dx < 0 ? (-this.dx) : (this.dx);
                    this.dy = this.dy < 0 ? (-this.dy) : (this.dy);
                    if (this.dx > this.dy)
                    {
                        this.cumul = this.dx * 0.5;
                        this.i = 1;
                        while (this.i <= this.dx)
                        {
                            
                            this.x = this.x + this.xinc;
                            this.cumul = this.cumul + this.dy;
                            if (this.cumul >= this.dx)
                            {
                                this.cumul = this.cumul - this.dx;
                                this.y = this.y + this.yinc;
                            }
                            if (this.x > this.lx + this.shape.step || this.x < this.lx - this.shape.step || this.y > this.ly + this.shape.step || this.y < this.ly - this.shape.step)
                            {
                                this.lx = this.x;
                                this.ly = this.y;
                                this.DrawShape(this.x, this.y);
                            }
                            var _loc_2:String = this;
                            var _loc_3:* = this.i + 1;
                            _loc_2.i = _loc_3;
                        }
                    }
                    else
                    {
                        this.cumul = this.dy * 0.5;
                        this.i = 1;
                        while (this.i <= this.dy)
                        {
                            
                            this.y = this.y + this.yinc;
                            this.cumul = this.cumul + this.dx;
                            if (this.cumul >= this.dy)
                            {
                                this.cumul = this.cumul - this.dy;
                                this.x = this.x + this.xinc;
                            }
                            if (this.x > this.lx + this.shape.step || this.x < this.lx - this.shape.step || this.y > this.ly + this.shape.step || this.y < this.ly - this.shape.step)
                            {
                                this.lx = this.x;
                                this.ly = this.y;
                                this.DrawShape(this.x, this.y);
                            }
                            var _loc_2:String = this;
                            var _loc_3:* = this.i + 1;
                            _loc_2.i = _loc_3;
                        }
                    }
                }
                this.target.unlock();
            }
            return;
        }// end function

        public function DrawShape(MOUSE_UP:int, MOUSE_UP:int) : void
        {
            return;
        }// end function

    }
}
